<template>
  <div class="adduser">
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="name"
        name="name"
        label="姓名"
        placeholder="姓名"
        :rules="[{ required: true, message: '请填写姓名' }]"
      />
      <!-- <van-field
        v-model="sex"
        name="sex"
        label="性别"
        placeholder="性别"
        :rules="[{ required: true, message: '请填写性别' }]"
      />-->
      <van-field
        v-model="email"
        name="email"
        label="邮箱"
        placeholder="邮箱"
        :rules="[{ required: true, message: '请填写邮箱' }]"
      />
      <van-field
        v-model="telephone"
        name="telephone"
        label="联系方式"
        placeholder="联系方式"
        :rules="[{ required: true, message: '请填写联系方式' }]"
      />
      <!-- <van-field
        v-model="expectedReward"
        name="expectedReward"
        label="期望报酬"
        placeholder="期望报酬"
        :rules="[{ required: true, message: '请填写期望报酬' }]"
      />-->
      <div class="grade">
        <van-cell title="生日" class="van-field__label"></van-cell>
        <div class="right">
          <el-date-picker
            v-model="brithday"
            type="date"
            placeholder="选择日期时间"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </div>
      </div>
      <van-field
        v-model="bmiHistory"
        type="bmiHistory"
        name="bmiHistory"
        label="从业年限"
        placeholder="从业年限"
        :rules="[{ required: true, message: '请填写从业年限' }]"
      />
      <van-field
        v-model="education"
        type="education"
        name="education"
        label="学历"
        placeholder="学历"
        :rules="[{ required: true, message: '请填写学历' }]"
      />
      <van-field
        v-model="expertise"
        type="expertise"
        name="expertise"
        label="擅长领域"
        placeholder="擅长领域"
        :rules="[{ required: true, message: '请填写擅长领域' }]"
      />
      <van-field
        v-model="graduatedFrom"
        type="graduatedFrom"
        name="graduatedFrom"
        label="毕业学校"
        placeholder="毕业学校"
        :rules="[{ required: true, message: '请填写毕业学校' }]"
      />
      <van-field
        v-model="industryExperience"
        type="industryExperience"
        name="industryExperience"
        label="从业经历"
        placeholder="从业经历"
        :rules="[{ required: true, message: '请填写从业经历' }]"
      />
      <van-field
        v-model="major"
        type="major"
        name="major"
        label="个人专业"
        placeholder="个人专业"
        :rules="[{ required: true, message: '请填写个人专业' }]"
      />
      <van-field
        v-model="serviceDirection"
        type="serviceDirection"
        name="serviceDirection"
        label="服务方向"
        placeholder="服务方向"
        :rules="[{ required: true, message: '请填写服务方向' }]"
      />
      <div class="grade">
        <van-cell title="照片" class="van-field__label"></van-cell>
        <div class="right">
          <el-upload
            class="upload-demo"
            action="http://112.124.67.43:8088/iot/demand/fileUpload"
            :on-success="PhotoUrl"
            :limit="1"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </div>
      </div>
      <div class="grade">
        <van-cell title="简历" class="van-field__label"></van-cell>
        <div class="right">
          <el-upload
            class="upload-demo"
            action="http://112.124.67.43:8088/iot/demand/fileUpload"
            :on-success="personaluserResumeFileUrl"
            :limit="1"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </div>
      </div>
      <div class="Ifsubmit">
        <van-button size="normal" type="info" native-type="submit">添加</van-button>
        <van-button size="normal" type="info" @click="cancel">取消</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { ReqAddPersonal,ReqUserexit} from "../../../request/index";
export default {
  data() {
    return {
      name: "",
      username: "",
      email: "",
      sex: "",
      telephone: "",
      major: "",
      expectedReward: "",
      industryExperience: "",
      password: "",
      bmiHistory: "",
      brithday: "",
      education: "",
      expertise: "",
      graduatedFrom: "",
      photoUrl: "",
      resumeFileUrl: "",
      serviceDirection: ""
    };
  },
  methods: {
    async onSubmit(values) {
        let data = {
        cooperativeUserId: sessionStorage.getItem("id"),
        email: this.form.email,
        password: this.form.pass,
        personalUserDetails: {
          bmiHistory: this.form.bmiHistory, //从业年限
          brithday: this.form.brithday, //生日
          education: this.form.education, //学历
          expertise: this.form.expertise, //擅长领域
          graduatedFrom: this.form.graduatedFrom, //毕业学校
          id: "",
          industryExperience: this.form.industryExperience, //从业经历
          major: this.form.major, //专业
          name: this.form.name,
          photoUrl: this.form.photoUrl, //照片
          resumeFileUrl: this.form.resumeFileUrl, //简历
          serviceDirection: this.form.serviceDirection, //服务方向
          status: 0
        },
        telephone: this.form.telephone,
        username: this.form.username
      };
      let username = {
        username: this.form.username
      };
      let userexit = await ReqUserexit(username);
      // console.log(userexit);
      if (userexit.code === 0) {
        let PersonalInfo = await ReqAddPersonal(data);
        // console.log(PersonalInfo);
        if (PersonalInfo.msg === "success") {
          this.$message({
            showClose: true,
            message: "恭喜你，添加成功",
            type: "success"
          });
          this.$router.go(0);
        } else {
          this.$message({
            showClose: true,
            message: "添加失败",
            type: "error"
          });
        }
      } else if (userexit.code === 500) {
        this.$message({
          showClose: true,
          message: "添加失败  用户名已存在",
          type: "error"
        });
      } else {
        this.$message({
          showClose: true,
          message: "添加失败",
          type: "error"
        });
      }
    },
    cancel() {
      this.$router.push("/self/partner/partIndex");
    },
    PhotoUrl(response, file, fileList) {
      this.photoUrl = response.msg;
    },
    personaluserResumeFileUrl(response, file, fileList) {
      this.resumeFileUrl = response.msg;
    }
  }
};
</script>

<style>
.adduser {
  width: 85%;
  margin: 0 auto;
}
.adduser .Ifsubmit {
  width: 160px;
  margin: 0 auto;
  margin-top: 20px;
  margin-bottom: 20px;
}
.adduser .Ifsubmit .van-button {
  margin-left: 15px;
}
.adduser .grade {
  display: flex;
  /* justify-content: space-between; */
  align-items: center;
}
.adduser .right {
  margin-left: 10px;
}
</style>